import Vue from 'vue';
import Vuex from 'vuex';

import getters from './getters';

import {
	enums,
	factuerManage,
	users
} from '@/api/my.js';
Vue.use(Vuex);

export default new Vuex.Store({
	state: {
		enums: {},
		factuer: [], //厂商
		userInfo: {},
		// 经纬度
		coord: {
			lat: '',
			lng: '',
		},
		equipTypeCode: uni.getStorageSync('equipTypeCode'), //当前设备类型code
	},
	mutations: {
		SET_ENUMS: (state, enums) => {
			state.enums = enums
		},
		//   厂商
		SET_FACTUER: (state, factuer) => {
			state.factuer = factuer
		},
		SET_USER_INFO: (state, userInfo) => {
			state.userInfo = userInfo
		},
		setCoord(state, coords) {
			state.coord.lat = coords.latitude;
			state.coord.lng = coords.longitude;
		},
		SET_EQUIP_TYPE_CODE: (state, equipTypeCode) => {
			state.equipTypeCode = equipTypeCode;
		},
	},
	actions: {
		enumsAll({
			commit
		}) {
			return new Promise((resolve, reject) => {
				enums().then((response) => {
					const {
						data
					} = response;
					commit('SET_ENUMS', data);
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},
		generatorFactuer({
			commit
		}) {
			return new Promise((resolve, reject) => {
				factuerManage('/all', 'post').then((response) => {
					const {
						data
					} = response;
					commit('SET_FACTUER', data);
					resolve(data)
				}).catch(error => {
					reject(error)
				})
			})
		},
		updateUserInfo({
			commit
		}) {
			return new Promise((resolve, reject) => {
				users(`/app/detail`, 'post').then((response) => {
					const {
						data
					} = response;
					commit('SET_USER_INFO', data);
					resolve(data)
				}).catch(error => {
					reject(error)
				})
			})
		},
		updateCoord: ({
			commit,
			state
		}) => {
			uni.getLocation({
				type: 'wgs84',
				success: (res) => {
					commit("setCoord", res);
				},
				fail: (err) => {
					console.log("err", err);

				}
			})
		},
	},
	getters,
})